package cn.itlaobing.springmvc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cn.itlaobing.springmvc.dao.StudentMapper;
import cn.itlaobing.springmvc.entity.Student;

@Service
public class StudentServiceImpl implements StudentService {

	@Autowired
	private StudentMapper studentMapper;

	
	/*
	 * private List<Student> students = new ArrayList<>();
	 * //无参构造 public StudentServiceImpl() { for (int i = 0; i < 20; i++) {
	 * Student student = new Student(); student.setId(i+1l);
	 * student.setBirthday(new Date()); student.setGender(Gender.Fmale);
	 * student.setName("name"+i); students.add(student); } }
	 */
	//分页带条件查询
	@Override
	public List<Student> findBySearchFilter(Map<String, String> searchFilter) {
		if(searchFilter==null){
			searchFilter=new HashMap<>();
		}else{
			if(StringUtils.isBlank(searchFilter.get("pageNum"))){
				searchFilter.put("pageNum","1");
				
			}
			if(StringUtils.isBlank(searchFilter.get("pageSize"))){
				searchFilter.put("pageSize","1");
			}
		}		
		PageHelper.startPage(searchFilter); //map中需要包含 pageNum 和pageSize 
		return studentMapper.findBySearchFilter(searchFilter);
	}

	// 保存添加
	@Override
	public void save(Student student) {
		studentMapper.insert(student);
	   // student.setId((RandomUtils.nextLong())); students.add(student);
		//studentDao.save(student);
	}

	@Override
	public Student findById(int id) {
		
		/*
		 * for(Student student:students){
		 * if(student.getId().longValue()==id.longValue()){ return student; } }
		 * return null;
		 */
		return studentMapper.selectByPrimaryKey(id);//studentDao.findById(id);
	}

	// 修改
	@Override
	public void update(Student student) {
		studentMapper.updateByPrimaryKey(student);
		/*
		 * Student stu=this.findById(student.getId()); if(stu!=null){
		 * stu.setName(student.getName()); stu.setGender(student.getGender());
		 * stu.setBirthday(student.getBirthday()); }
		 */
		//studentDao.update(student);
	}

	@Override
	public void delete(int id) {
		studentMapper.deleteByPrimaryKey(id);
		/*
		 * for(Student student:students){
		 * if(student.getId().longValue()==id.longValue()){
		 * students.remove(student); return; } }
		 */
		//studentDao.delete(id);
	}

}
